local DB = require("common.DB")
local LuaMongo = _G.lua_mongo
local Config = require("Config")
local RoleDBLogic = require("role.RoleDBLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local Human = require("core.Human")
local RoleFFI = require("role.RoleFFI")
local InnerDataManagerFW = require("innerdata.InnerDataManagerFW")
local InnerDataDefine = require("innerdata.Define")
local GmCommon = require("chat.GmCommon")
local HorseRankLogic = require("rank.HorseRankLogic")
local LvRankLogic = require("rank.LvRankLogic")
local PowerRankLogic = require("rank.PowerRankLogic")
local SbRankLogic = require("rank.SbRankLogic")
local PetRankLogic = require("rank.PetRankLogic")
local BaowuRankLogic = require("rank.BaowuRankLogic")
local PataRankLogic = require("rank.PataRankLogic")
local FamilyManager = require("family.FamilyManager")
local ComplexSrvActFW = require("activity.ComplexSrvActFW")
local OperateActFW = require("activity.OperateActFW")
local ActDefine = require("activity.Define")

function changeRankName(human, rank)
    for i = 1, #rank do
        if human._id == rank[i].uuid then
            rank[i].name = human.name
            break
        end
    end
end

function dealRank(human)
    changeRankName(human, HorseRankLogic.HORSE_RANK)
    changeRankName(human, LvRankLogic.LEVEL_RANK)
    changeRankName(human, PowerRankLogic.POWER_RANK)
    changeRankName(human, SbRankLogic.SHENBING_RANK)
    changeRankName(human, PetRankLogic.PET_RANK)
    changeRankName(human, PataRankLogic.PATA_RANK)
    changeRankName(human, BaowuRankLogic.BAOWU_RANK)
end

function changeName(human, newName)
    local nameLen = string.len(newName)
    if nameLen < 2 or nameLen > 24 then        
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.ROLE_NAME_TOO_LONG)
    end

    if newName ~= GmCommon.filter_name(newName) then        
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.ROLE_NAME_HAVE_ERROR)
    end

    if RoleDBLogic.isNameExistInDB(newName) then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.ROLE_NAME_EXIST)
    end

    InnerDataManagerFW.Send2LInnerData(human, InnerDataDefine.INNERDATA_TYPE_CHANGE_NAME, {name = newName, type = 1})
end

function dealFamily(human)
    local family = FamilyManager.findByUuid(human.familyUuid)
    if not family then return end
    local member = FamilyManager.getMember(family, human._id)
    if not member then return end
    member.name = human.name  
end

function dealAct(human)
    local data = ComplexSrvActFW.onGetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK)
    if data then
        for k,v in pairs(data) do
            if k == human._id then
                v[1] = human.name
            end
        end
    end
    ComplexSrvActFW.onSetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK, RECHARGE_DBDATA)
    require("activity.complex.ChongzhiRank").onChangeRankeName(human._id, human.name)

    local actData = OperateActFW.onGetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK)
    if actData then
        for k,v in pairs(actData) do
            if k == human._id then
                v[1] = human.name
            end
        end
    end
    OperateActFW.onSetActDBData(ActDefine.OPERATEACT_TYPE_CHONGZHIRANK, RECHARGE_DBDATA)
    require("activity.operate.ChongzhiRank").onChangeRankeName(human._id, human.name)
end

function gaimingSucc(human, oType, info)
    Human.onlineHuman[human.name] = nil
    human.db.name = info.name
    human.name = info.name
    Human.onlineHuman[info.name] = human  
    dealRank(human)
    dealFamily(human)
    dealAct(human)
    local sendMsg = Msg.gc.GC_CHANGE_NAME_SUCCESS
    sendMsg.name = human.name
    Msg.send(sendMsg, human.fd)
end

InnerDataManagerFW.Register(InnerDataDefine.INNERDATA_TYPE_CHANGE_NAME, gaimingSucc)
